package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.Enum.EnumNextProcess;
import com.mf.Enum.EnumWorkType;
import com.mf.constant.ConstantBasic;
import com.mf.entity.dto.*;
import com.mf.entity.req.ReqRelevancyById;
import com.mf.entity.req.search.SearchReqIncomingDetail;
import com.mf.entity.req.search.SearchReqSortOut;
import com.mf.exception.GraceException;
import com.mf.service.*;
import com.mf.utils.UtilEnumNextProcess;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import com.mf.entity.req.search.SearchReqSmashDetail;
import com.mf.mapper.MapperSmashDetail;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ImplServiceSmashDetail extends ServiceImpl<MapperSmashDetail, DtoSmashDetail> implements ServiceSmashDetail {
    @Resource
    private ServiceSmash serviceSmash;


    @Resource
    private ServiceIncomingDetail serviceIncomingDetail;

    @Resource
    private ServiceRiveDetail serviceRiveDetail;

    /**
     * 参数查询条件
     */
    @Override
    public LambdaQueryWrapper<DtoSmashDetail> fillParams(SearchReqSmashDetail params) {
        return null;
    }

    /**
     * 查询粉碎明细数量
     */
    @Override
    public long countBySmashId(Integer smashId) {
        LambdaQueryWrapper<DtoSmashDetail> wrapper = loadQw();
        wrapper.eq(DtoSmashDetail::getSamshId, smashId);
        return count(wrapper);
    }

    /**
     * 保存粉碎明细
     */
    @Override
    @Transactional
    public void saveSmashDetail(List<Integer> ids, Integer smashId, Integer sourceId, Integer workType) {
        DtoSmash smash = serviceSmash.getById(smashId);
        if (smash == null) {
            GraceException.display("粉碎工单【" + smashId + "】不存在");
        }
        List<DtoSmashDetail> dtoSmashDetails = new ArrayList<>();

        // 来源为来料单
        if (EnumWorkType.IN.getCode().equals(workType)) {
            // 来料单详情
            Map<Integer, DtoIncomingDetail> detailMap = serviceIncomingDetail.getDtoIncomingDetailMap(sourceId,
                    workType);

            List<ReqRelevancyById> relevancyByIds = new ArrayList<>();

            for (Integer id : ids) {
                serviceIncomingDetail.setRelevancyList(sourceId, id, detailMap, relevancyByIds);
                DtoIncomingDetail incomingDetail = detailMap.get(id);
                DtoSmashDetail riveDetail = DtoSmashDetail.builder()
                        .samshId(smashId)
                        .sourceId(id)
                        .sourceType(workType)
                        .materailName(incomingDetail.getMaterialInfo())
                        .materailId(incomingDetail.getMaterialId())
                        .materailWeight(incomingDetail.getGrossWeight())
                        .build();
                dtoSmashDetails.add(riveDetail);
            }

            serviceIncomingDetail.setRelevancy(relevancyByIds, sourceId, EnumWorkType.SMASH.getCode());
        } else if (EnumWorkType.RIVE.getCode().equals(workType)) {
            final Map<Integer, DtoRiveDetail> dtoRiveDetailMap = serviceRiveDetail.getDtoRiveDetailMap(sourceId, workType);
            List<ReqRelevancyById> relevancyByIds = new ArrayList<>();
            for (Integer id : ids) {
                if (!dtoRiveDetailMap.containsKey(id)) {
                    GraceException.display("撕碎明细" + id + "不存在");
                }

                relevancyByIds.add(new ReqRelevancyById(id, sourceId));
                DtoRiveDetail dtoRiveDetail = dtoRiveDetailMap.get(id);
                DtoSmashDetail smashDetail = DtoSmashDetail.builder()
                        .samshId(smashId)
                        .sourceId(id)
                        .sourceType(workType)
                        .materailName(dtoRiveDetail.getMaterailName())
                        .materailId(dtoRiveDetail.getMaterialId())
                        .materailWeight(Math.ceil(dtoRiveDetail.getMaterailWeight() * smash.getOutRatio() / 100))
                        .build();

                dtoSmashDetails.add(smashDetail);
            }

            serviceRiveDetail.setRelevancy(relevancyByIds, sourceId, EnumWorkType.SMASH.getCode());

        } else {
            GraceException.display("未知的工单类型");
        }
        this.saveBatch(dtoSmashDetails);

    }

}